package com.xms.security.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xms.core.config.FinalValueDefaults;
import com.xms.security.api.model.SetTeamRolesModel;
import com.xms.security.api.model.SetUserRolesModel;
import com.xms.security.core.RoleMemberShipType;
import com.xms.security.entity.RoleMemberShip;
import com.xms.security.service.role.IRoleMemberShipService;
import com.xms.utils.CollectionUtil;
import com.xms.utils.UUIDUtil;
import com.xms.web.framework.controller.ApiBaseController;
import com.xms.web.framework.model.JsonResultObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * <p>
 * AssignRolesController
 * </p>
 *
 * @author migo
 * @since 2021/10/9
 */
@RestController
@RequestMapping("/{org}/api/security/assignroles")
public class AssignRolesController extends ApiBaseController {
    private IRoleMemberShipService _roleMemberShipService;

    @Autowired
    public void AssignRolesController(IRoleMemberShipService roleMemberShipService) {
        _roleMemberShipService = roleMemberShipService;
    }

    @ApiOperation("重设用户角色")
    @PostMapping("touser")
    public JsonResultObject assignRolesToUser(@RequestBody SetUserRolesModel model) {
        if (CollectionUtil.isEmpty(model.userId)) {
            return JNotSpecifiedRecord();
        }
        List<RoleMemberShip> roleMemberShips = new ArrayList<>();
        for (UUID uid : model.userId) {
            if (CollectionUtil.notEmpty(model.roleId)) {
                for (UUID rid : model.roleId) {
                    //过滤掉管理员
                    if (rid.equals(FinalValueDefaults.ADMIN_ROLEID) && uid.equals(FinalValueDefaults.ADMIN_USERID))
                        continue;
                    RoleMemberShip entity = new RoleMemberShip() {{
                        setRoleid(rid);
                        setMemberid(uid);
                        setTypecode(RoleMemberShipType.USER.value());
                        setRolemembershipid(UUIDUtil.get());
                    }};
                    roleMemberShips.add(entity);
                }
            }
        }
        if (CollectionUtil.isEmpty(roleMemberShips)) {
            return JSaveFailure();
        }
        boolean success = _roleMemberShipService.remove(new LambdaQueryWrapper<RoleMemberShip>()
                .eq(RoleMemberShip::getTypecode, RoleMemberShipType.USER.value())
                .in(RoleMemberShip::getMemberid, roleMemberShips.stream().map(x -> x.getMemberid()).collect(Collectors.toList())));
        success = _roleMemberShipService.saveBatch(roleMemberShips);
        return success ? JSaveSuccess() : JSaveFailure();
    }

    @ApiOperation("设置团队安全角色")
    @PostMapping("toteam")
    public JsonResultObject assignRolesToTeam(@RequestBody SetTeamRolesModel model) {
        if (CollectionUtil.isEmpty(model.teamId)) {
            return JNotSpecifiedRecord();
        }
        List<RoleMemberShip> roleMemberShips = new ArrayList<>();
        for (UUID tid : model.teamId) {
            if (CollectionUtil.notEmpty(model.roleId)) {
                for (UUID rid : model.roleId) {
                    RoleMemberShip entity = new RoleMemberShip() {{
                        setRoleid(rid);
                        setMemberid(tid);
                        setTypecode(RoleMemberShipType.TEAM.value());
                        setRolemembershipid(UUIDUtil.get());
                    }};
                    roleMemberShips.add(entity);
                }
            }
        }
        if (CollectionUtil.isEmpty(roleMemberShips)) {
            return JSaveFailure();
        }
        boolean success = _roleMemberShipService.remove(new LambdaQueryWrapper<RoleMemberShip>()
                .eq(RoleMemberShip::getTypecode, RoleMemberShipType.TEAM.value())
                .in(RoleMemberShip::getMemberid, roleMemberShips.stream().map(x -> x.getMemberid()).collect(Collectors.toList())));
        success = _roleMemberShipService.saveBatch(roleMemberShips);
        return success ? JSaveSuccess() : JSaveFailure();
    }
}
